{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "from datetime import timedelta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "now = datetime.now()\n",
    "# now\n",
    "now.year, now.month, now.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff_dt = datetime(2018, 8, 31) - datetime(2018, 8, 30, 23, 0)\n",
    "diff_dt.days, diff_dt.seconds, type(diff_dt), now + timedelta(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fromat, convert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(now), now.strftime('%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "value = '2018-08-30 18:28:47'\n",
    "dt1 = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')\n",
    "values = ['2018/08/30', '2018/08/31']\n",
    "dt2 = [datetime.strptime(x, '%Y/%m/%d') for x in values] \n",
    "dt1, dt2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## index, split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = [datetime(2018, 9, x) for x in range(1, 7)]\n",
    "ts = pd.Series(data = np.random.randn(6), index=dates)\n",
    "dates, ts, type(ts.index), type(ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [start:end:step]\n",
    "ts.index[0], ts[::2], ts['2018-9-2':'2018-9-5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts['2018-9-3':'2018-9-5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.truncate(before='2018-09-03', after='2018-09-5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt3 = pd.date_range(start='15/6/2016', periods=1000, freq='W-WED')\n",
    "type(dt3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = pd.DataFrame(data = np.random.randn(1000, 4), index=dt3, columns=['col' + str(x) for x in range(1, 5)])\n",
    "df3[:5], df3.tail(5), df3.index.size, df3.columns.size, df3.size\n",
    "df3.loc['2022-09'] # df3.loc['9-2022']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## duplicate indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt4 = pd.DatetimeIndex(data=['1/9/2018', '2/9/2018', '2/9/2018', '2/9/2018', '3/9/2018'])\n",
    "df4 = pd.Series(data=np.arange(5), index=dt4)\n",
    "dt4.is_unique, df4.index.is_unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4['1/9/2018'], df4['2/9/2018'], df4['3/9/2018']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4_grouped = df4.groupby(level=0) # index only one level\n",
    "df4_grouped.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts2 = ts[::2]\n",
    "ts2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts3 = ts2.resample('D')\n",
    "ts3.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt5 = pd.date_range('1/9/2018', periods=12, freq='T')\n",
    "s5 = pd.Series(data=np.arange(12), index=dt5)\n",
    "s5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s5_1 = s5.resample(rule='5min')\n",
    "s5_1.mean()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
